#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "eloop.h"
#include "cap.h"
#include "sig.h"
#include "version.h"

static void usage(void)
{
	printf("usage:\n"
	       "  m11 [-i<ifname>] [-B<buffer size>] [-o<savefile.pcap>]"
	       "\n");

	printf("options:\n"
	       "  -h = show this help text\n"
	       "  -i = interface name like wlan0, moni0 etc.\n"
	       "  -B = buffer size KB to store all packets captured\n"
	       "  -o = output to file must like *.pcap\n"
	       "  -v = show version\n");

	printf("example:\n"
	       "  ifconfig wlan0 down\n"
	       "  iw dev wlan0 set type monitor\n"
	       "  ifconfig wlan0 up\n"
	       "  m11 -i wlan0 -B 2048 -o ./tmp.pcap\n"
	       "  press CTRL+C to finish and store packets in tmp.pcap\n");
}

static void version(void)
{
	printf("m11 %s\n", M11_VERSION);
	/* M11_BUILD_PROPERTY is generated by Makefile */
	printf("built by %s\n", M11_BUILD_PROPERTY);
}

int main(int argc, char *argv[])
{
	int c, ret;

	for (;;) {
		c = getopt(argc, argv, "hi:B:o:v");
		if (c < 0)
			break;
		switch (c) {
		case 'h':
			usage();
			return 0;
		case 'i':
			cap_set_ifname(optarg);
			break;
		case 'B':
			cap_set_bufsz(atoi(optarg));
			break;
		case 'o':
			cap_set_savefile(optarg);
			break;
		case 'v':
			version();
			return 0;
		default:
			usage();
			return 0;
		}
	}

	ret = cap_open();
	if (ret)
		return ret;
	register_signal_terminate();
	eloop_run();
	cap_close();

	return 0;
}
